package com.wangan;

import com.princexml.wrapper.Prince;
import sun.awt.OSInfo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        // 获取 java 版本
        String version = System.getProperty("java.specification.version");

        // 获取系统类型
        String platform = System.getProperty("os.name", "");
        platform = platform.toLowerCase().contains("window") ? "win" : "linux";

        // 当前程序目录
        String current = System.getProperty("user.dir");

        System.out.println(String.format("current=%s", current));

        // html 文件路径
        File index = Paths.get(current, "..", "index.html").toFile();
        if (!index.exists()) {
            System.out.println(String.format("file not exist,file=%s", index.getAbsolutePath()));
            return;
        }
        String command = Paths.get(current, "..", "prince-15.2-win64", "bin", "prince.exe").toString();
        if (OSInfo.getOSType() == OSInfo.OSType.LINUX) {
            command = "prince";
        }
        Prince prince = new Prince(command);
        // prince.setLog("/path/to/log.txt");
        // prince.addStyleSheet("/path/to/stylesheet.css");
        // prince.addScript("/path/to/script.js");
        prince.setJavaScript(true);

        try {
            // 转换 html 文件
            File file = Paths.get(current, String.format("java%s_%s.pdf", version, platform)).toFile();
            prince.convert(index.getAbsolutePath(), file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}